home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / comm2 / xbtx.lha / Source / IOFile.cpp < prev    next >
C/C++ Source or Header  |  1995-10-13  |  4KB  |  154 lines

  1. /*
  2. **    $Id: IOFile.cpp 1.5 1995/10/13 11:43:06 olsen Exp olsen $
  3. **
  4. **    :ts=4
  5. */
  6.  
  7. /*
  8.  * Copyright © 1995 by Olaf Barthel, All Rights Reserved
  9.  *
  10.  * Redistribution and use in source and binary forms, with or without
  11.  * modification, are permitted provided that the following conditions
  12.  * are met:
  13.  * 1. Redistributions of source code must retain the above copyright
  14.  *    notice, this list of conditions and the following disclaimer.
  15.  * 2. Redistributions in binary form must reproduce the above copyright
  16.  *    notice, this list of conditions and the following disclaimer in the
  17.  *    documentation and/or other materials provided with the distribution.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
  20.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
  22.  * EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  25.  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26.  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  27.  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  28.  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29.  *
  30.  * This software has not been validated by the ``Bundesamt fuer Zulassungen in
  31.  * der Telekommunikation'' of the ``Deutsche Bundepost Telekom'' and thus
  32.  * must not be used for accessing the BTX-Network of the Telekom in Germany.
  33.  *
  34.  * Diese Software hat keine Zulassung durch das Bundesamt fuer Zulassungen in
  35.  * der Telekommunikation der Deutschen Bundespost Telekom und darf daher nicht
  36.  * am Netz der Deutschen Bundespost Telekom in Deutschland betrieben werden.
  37.  */
  38.  
  39. /****************************************************************************/
  40.  
  41. #include <exec/memory.h>
  42. #include <dos/dosextens.h>
  43. #include <dos/stdio.h>
  44.  
  45. #include <clib/exec_protos.h>
  46. #include <clib/dos_protos.h>
  47.  
  48. #ifdef __SASC
  49. #include <pragmas/exec_pragmas.h>
  50. #include <pragmas/dos_pragmas.h>
  51.  
  52. extern struct ExecBase *SysBase;
  53. extern struct DosLibrary *DOSBase;
  54. #endif    // __SASC
  55.  
  56. /****************************************************************************/
  57.  
  58. #ifndef _IOFILE_HPP
  59. #include "IOFile.hpp"
  60. #endif
  61.  
  62. /****************************************************************************/
  63.  
  64. /* this macro lets us long-align structures on the stack */
  65. #define D_S(type,name) char a_##name[sizeof(type)+3]; \
  66.                        type *name = (type *)((LONG)(a_##name+3) & ~3);
  67.  
  68. IOFile::IOFile()
  69. {
  70.     FileHandle = NULL;
  71. }
  72.  
  73. IOFile::~IOFile()
  74. {
  75.     Close();
  76. }
  77.  
  78. LONG IOFile::Open(CONST STRPTR Channel,ULONG Unit,ULONG Baud,BOOL RTS_CTS)
  79. {
  80.     LONG Error;
  81.  
  82.     Close();
  83.  
  84.     if(FileHandle = ::Open((char *)Channel,MODE_OLDFILE))
  85.     {
  86.         BPTR FileLock = Lock((char *)Channel,SHARED_LOCK);
  87.  
  88.         if(FileLock)
  89.         {
  90.             D_S(struct FileInfoBlock,FileInfo);
  91.  
  92.             if(Examine(FileLock,FileInfo))
  93.             {
  94.                 Size = FileInfo->fib_Size;
  95.                 BytesRead = 0;
  96.  
  97.                 UnLock(FileLock);
  98.  
  99.                 SetVBuf(FileHandle,NULL,BUF_FULL,4096);
  100.  
  101.                 return(0);
  102.             }
  103.             else
  104.                 Error = IoErr();
  105.  
  106.             UnLock(FileLock);
  107.         }
  108.         else
  109.             Error = IoErr();
  110.  
  111.         Close();
  112.     }
  113.     else
  114.         Error = IoErr();
  115.  
  116.     return(Error);
  117. }
  118.  
  119. VOID IOFile::Close(VOID)
  120. {
  121.    if(FileHandle)
  122.    {
  123.        ::Close(FileHandle);
  124.        FileHandle = NULL;
  125.    }
  126. }
  127.  
  128. LONG IOFile::GetChar(LONG Timeout)
  129. {
  130.     if(FileHandle)
  131.     {
  132.         LONG Result = FGetC(FileHandle);
  133.  
  134.         if(Result < 0)
  135.             return(CHANNELERR_EOF);
  136.         else
  137.         {
  138.             BytesRead++;
  139.  
  140.             return(Result);
  141.         }
  142.     }
  143.     else
  144.         return(CHANNELERR_EOF);
  145. }
  146.  
  147. LONG IOFile::Waiting(VOID)
  148. {
  149.     if(FileHandle)
  150.         return(Size - BytesRead);
  151.     else
  152.         return(0);
  153. }
  154.